﻿using Anke.SHManage.BLL;
using Anke.SHManage.MSSQLDAL.TJDAL;
using Microsoft.Reporting.WebForms;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Anke.SHManage.Web.Reports
{
    public partial class PersonalPRModeWebForm : System.Web.UI.Page
    {
        public string whichonez
        {
            get
            {
                object o = ViewState["whichonez"];
                return (o == null) ? String.Empty : (string)o;
            }
            set
            {
                ViewState["whichonez"] = value;
            }
        }

       
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                
                //通过个人工号取分站，如果有出车的分站，就是个人，没有就取分中心，如果有分中心就看分中心，没有就是全中心
                string selfWorkCode = UserOperateContext.Current.Session_UsrInfo.WorkCode;
                string selfStationID = new PersonalStatisticsBLL().GetstaionCodeByWorkCodeTAmbulance(selfWorkCode);
                string selfCenterID = UserOperateContext.Current.Session_UsrInfo.P_Department.DispatchSubCenterID;
                if (selfStationID != null && selfStationID != "")
                {
                    whichonez = "个人";
                }
                else if (selfCenterID != null && selfCenterID != "")
                {
                    whichonez = "分中心";
                }
                else
                {
                    whichonez = "全中心";
                }

               
                InitPage(whichonez);
            }


        }
        //public string Getstr()
        //{
        //    return whichone;
        //}
        //个人
        
        protected void btnSearch_Click(object sender, EventArgs e)
        {
           
            if (whichonez == "分中心")
            {
                Response.Write("<script>alert('您只能查看分中心。');</script>");

            }
            if (whichonez == "全中心")
            {
                Response.Write("<script>alert('您只能查看中心。');</script>");

            }
            if (whichonez == "个人")
            {
                string whichone = "个人";
                BindReportDataSource(whichone);
            }
        }
        //分站
        protected void btnSearch_Click1(object sender, EventArgs e)
        {
            
            if (whichonez == "分中心")
            {
                Response.Write("<script>alert('您只能查看分中心。');</script>");

            }
            if (whichonez == "全中心")
            {
                Response.Write("<script>alert('您只能查看中心。');</script>");

            }
            if (whichonez == "个人")
            {
                string whichone = "分站";
                BindReportDataSource(whichone);
            }
        }
        //分中心
        protected void btnSearch_Click2(object sender, EventArgs e)
        {
           
            if (whichonez == "全中心")
            {
                Response.Write("<script>alert('您只能查看中心。');</script>");

            }
            if (whichonez == "分中心")
            {
                string whichone = "分中心";
                BindReportDataSource(whichone);
            }
        }
        //全中心
        protected void btnSearch_Click3(object sender, EventArgs e)
        {

            string whichone = "全中心";
            BindReportDataSource(whichone);
        }

        private void InitPage(string whichone)
        {
            this.StartDate.Text = DateTime.Now.ToString("yyyy-MM-01 00:00:00");
            this.EndDate.Text = DateTime.Now.ToString("yyyy-MM-dd 23:59:59");

            BindReportDataSource(whichone);
        }
        protected void BindReportDataSource(string whichone)
        {

            DateTime dateStart = Convert.ToDateTime(this.StartDate.Text);
            DateTime dateEnd = Convert.ToDateTime(this.EndDate.Text);
            string start1 = dateStart.ToString();
            string end1 = dateEnd.ToString();

            string workID = "";
            string center = "";
            string Station = "";

            if (whichone == "个人")
            {
                workID = UserOperateContext.Current.Session_UsrInfo.WorkCode;
                center = "";
                Station = "";
            }
            if (whichone == "分站")
            {
                workID = "";
                string workidTemp = UserOperateContext.Current.Session_UsrInfo.WorkCode;
                Station = new TJDAL().GetstaionCodeByWorkCodeTAmbulance(workidTemp);
                center = "";
            }
            if (whichone == "分中心")
            {
                workID = "";
                Station = "";
                center = UserOperateContext.Current.Session_UsrInfo.P_Department.DispatchSubCenterID;
            }
            if (whichone == "全中心")
            {
                workID = "";
                center = "";
                Station = "";
            }

            this.ReportViewer1.LocalReport.DataSources.Clear();
            this.ReportViewer1.LocalReport.ReportPath = Server.MapPath(@"~/Reports/PersonalPR.rdlc");
            Microsoft.Reporting.WebForms.ReportParameter st = new Microsoft.Reporting.WebForms.ReportParameter("StartTime", start1);
            Microsoft.Reporting.WebForms.ReportParameter ed = new Microsoft.Reporting.WebForms.ReportParameter("EndTime", end1);
            Microsoft.Reporting.WebForms.ReportParameter zz = new Microsoft.Reporting.WebForms.ReportParameter("Name", whichone);
            this.ReportViewer1.LocalReport.SetParameters(new Microsoft.Reporting.WebForms.ReportParameter[] { st, ed, zz });


            TJDAL dal = new TJDAL();

            DataTable dt = dal.getPersonalPR(dateStart, dateEnd, center, Station, workID, whichone);
            this.ReportViewer1.LocalReport.DataSources.Add(new ReportDataSource("DataSet1", dt));
            DataTable dt1 = dal.getPersonalPRTimes(dateStart, dateEnd, center, Station, workID, whichone);
            this.ReportViewer1.LocalReport.DataSources.Add(new ReportDataSource("DataSet2", dt1));


            this.ReportViewer1.LocalReport.Refresh();
        }

    }
}